package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Items;
import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.service.Impl.Shopping.ItemsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.OrderServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.ItemsService;
import cdu.javaendwork.service.Interface.Shopping.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

@WebServlet("/items_delete")
public class ItemsDeleteController extends HttpServlet {
    ItemsService itemsService = new ItemsServiceImpl();
    OrderService orderService = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到需要删除的条目id
        String iid = req.getParameter("itemsid");
        int itemsid = Integer.parseInt(iid);
        //拿到页码
        String sPage=req.getParameter("page");
        int page=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);
        //获得要删除的items信息和购物车信息
        Items items = itemsService.findById(itemsid);
        Order craft = orderService.findById(items.getOrderid());
        //删除items
        itemsService.delete(items.getId());
        //计算修改价格
        BigDecimal nowCraftPrice = new BigDecimal(craft.getPrice().doubleValue()-items.getPrice().doubleValue());
        //修改craft价格
        orderService.update(craft.getId(),craft.getUserid(),1,new Timestamp(new Date().getTime()),
                nowCraftPrice,"");
        //修改完成回到原页面的原页码
        req.getRequestDispatcher("craft").forward(req,resp);
    }
}
